package demo;

import java.net.Socket;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import model.PropertyInfo;
import model.User;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class DataDisposeThread implements Runnable {
	private Socket socket = null;

	public DataDisposeThread(Socket socket) {

		this.socket = socket;
	}

	@SuppressWarnings("unchecked")
	public void run() {

		HashMap<Object, Object> objout = new HashMap<Object, Object>();

		/** ************************************************* */

		objout.put("zhy", "じ☆ve雪");
		objout.put("sex", "女");
		objout.put("age", 21);
		objout.put("info", "じ☆ve弦的老婆");

		/** ************************************************* */

		AMF3 amf = new AMF3();

		System.out.println("----向Flash客户端写入Object对象----");

		User users = new User();
		users.setId(Long.valueOf(100));
		users.setPassport("123");
		users.setPassword("阿斯顿发asdf 123-=");
		List<User> list = new ArrayList<User>();
		list.add(users);
		list.add(users);
		// amf.sendAmfData(list, socket);
		Map<String, Object> maps = new HashMap<String, Object>();
		maps.put("list", list);
		maps.put("user", users);
		maps.put("str", "asdfasf");
		maps.put("date", new Date());
		maps.put("long", Long.valueOf(1000000091));
		maps.put("null", null);
		maps.put("map", objout);
		PropertyInfo propertyInfo = new PropertyInfo();
		propertyInfo.setCode(1);
		propertyInfo.setData(maps);
		// 测试向客户端发送一堆数据
		// amf.sendAmfData(propertyInfo, socket);
		// amf.sendAmfData(objout, socket);// 向连接的客户端发送数据对象

		System.out.println("发送去的数据为：" + objout.get("zhy"));

		System.out.println("----向Flash客户端读取Object对象----");

		// //理解////
		// HashMap objin = (HashMap) amf.receiveAmfData(socket);
		while (true) {
			PropertyInfo propertyInfo2 = (PropertyInfo) amf
					.receiveAmfData(socket);
			JDBCDao dao = new JDBCDao();
			if (propertyInfo2 != null) {
				dao.solveInfo(propertyInfo2, socket, amf);
				System.out.println(propertyInfo2.getCode());
			}

			// for (Iterator iter = objin.keySet().iterator(); iter.hasNext();)
			// {
			//
			// Object key = iter.next();
			//
			// System.out.println("接收到的数据为：" + objin.get(key));// 向连接的客户端接收数据对象
			//
			// }
		}

	}
}
